import Vue from 'vue'
import Router from 'vue-router'
import {getUserInfo} from '../util/requestAll/user'
import {getCookie, setCookie} from '../util/cookieUtil'
import store from '../store'

Vue.use(Router)

const router =new Router({
  routes: [
    {
      path: '/',
      redirect: '/signIn'
    },
    {
      path: '/signIn',
      name: 'SignIn',
      meta: { title: '登录' },
      component: ()=>import('@/components/sign/signin/SignIn.vue')
    },
    {
      path: '/signUp',
      name: 'SignUp',
      meta: { title: '注册' },
      component: ()=>import('@/components/sign/signup/SignUp.vue')
    },
    {
      path: '/index',
      name: 'Index',
      component: ()=>import('@/components/Index.vue'),
      meta: { title: '登陆之后的主页' },
      redirect: '/index/message',
      children: [
        {
          path: '/index/message',
          name: 'Message',
          meta: { title: '消息页面' },
          component: ()=>import('@/components/message/Message.vue')
        },
        {
          path: '/index/contacts',
          name: 'Contacts',
          meta: { title: '联系人页面' },
          component: ()=>import('@/components/contacts/Contacts.vue')
        },
        {
          path: '/index/news',
          name: 'News',
          meta: { title: '消息页面' },
          component: ()=>import('@/components/news/News.vue')
        },
        {
          path: '/index/wechatMoments',
          name: 'WechatMoments',
          meta: { title: '登陆之后的主页' },
          component: ()=>import('@/components/wechatMoments/WechatMoments.vue')
        },
      ]
    },
    {
      path: '/hallChat',
      name: 'HallChat',
      meta: { title: '大厅' },
      component: ()=>import('@/components/message/HallChat')
    },
    {
      path: '/AddFriend',
      name: 'AddFriend',
      meta: { title: '搜索好友/群页面' },
      component: ()=>import('@/components/contacts/AddFriend')
    },
    {
      path: '/SendApplyFriend',
      name: 'SendApplyFriend',
      meta: { title: '添加好友页面' },
      component: ()=>import('@/components/contacts/SendApplyFriend')
    },
    {
      path: '/ApplyFromFriend',
      name: 'ApplyFromFriend',
      meta: { title: '好友请求页面' },
      component: ()=>import('@/components/contacts/ApplyFromFriend')
    },
    {
      path: '/requestErr',
      name: 'requestErr',
      meta: { title: '错误' },
      component: ()=>import('@/components/error/requestErr')
    },
  ]
})
/**
 * beforeEach()在路由之前执行（跳转页面之前）
 * to:router即将进入的路由对象
 * from:当前导航即将离开的路由
 * next:Function,进行管道中的一个钩子，如果执行完了，则导航的状态就是 confirmed （确认的）；否则为false，终止导航
 */
router.beforeEach((to, from, next) => {
  if (getCookie('token')) {
    if(store.state.userInfo.id<0){//当前会话是否未被刷新
      // console.log('qq')
      getUserInfo({}).then(res=>{
        let resData=res.data
        if (resData.resStatus == 1) { //判断是否登陆
          store.commit('setId',resData.id)
          store.commit('setUserName',resData.username)
          store.commit('setPassWard',resData.passward)
          store.commit('setPhone',resData.phone)
          store.commit('setEmail',resData.email)
          store.commit('setNickName',resData.nickname)
          setCookie("token",resData.sessionId)
          next();
        }else{
          setCookie("token","")
          next()
        }
      }).catch(err=>{
        next()
      })
    }else{
      // console.log('qq1')
      next();//当前会话未被刷新，则继续进入
    }
  }else{
    if(to.path=='/'||to.path=='/signIn'||to.path=='/signUp'||to.path=='/requestErr'){
      next()
    }else{
      next('/signIn')
    }
  }
})

export default router
